class Solution {
//    public int searchInsert(int[] nums, int target) {
//        int left = 0;
//        int right = nums.length-1;
//        int mid;
//        while (left <= right){
//            mid = ((right - left) >> 1) + left;
//            if(nums[mid] >= target){
//                right = --mid;
//            }else{
//                left = ++mid;
//            }
//        }
//        return ++right;
//    }


    //二刷
    public int searchInsert(int[] nums, int target) {
        int l = nums.length;
        int left = 0;
        int right = l;

        //[ , )
        while (left < right){
            int mid = left + (right - left)/2;
            if(nums[mid] > target){
                right = mid;
            }else if(nums[mid] < target){
                left = mid + 1;
            }else return mid;
        }
        // left=right [left,right)
        return left;
    }



}